package cn.picclife.mwx.manager.sys.controller;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;

import cn.picclife.mwx.manager.sys.model.bo.DataResponse;
import cn.picclife.mwx.manager.sys.model.bo.PageCount;
import cn.picclife.mwx.manager.sys.model.bo.PageResponse;
import cn.picclife.mwx.manager.sys.model.bo.SysMaintainVUrlReq;
import cn.picclife.mwx.manager.sys.service.SysMaintainHtmlService;
import cn.picclife.mwx.manager.util.enums.ResponseStatusEnum;

@RestController
@RequestMapping("html")
public class SysMaintainHtmlController {

	private Log log = LogFactory.getLog(SysMaintainHtmlController.class);
	
	@Autowired
	private SysMaintainHtmlService sysMaintainHtmlServiceImpl;
	
	/**
	 * 分页查询
	 * @param req
	 * @return
	 */
	@RequestMapping(value="/queryHtml", method= {RequestMethod.POST})
	public PageResponse queryHtml(@RequestBody SysMaintainVUrlReq req) {
		
		log.info("===========queryHtml接口请求报文："+JSONObject.toJSON(req).toString());
		PageResponse pageResponse = new PageResponse();
		try {
			PageCount pageCount = sysMaintainHtmlServiceImpl.querySysHtml(req);
			pageResponse.setPageCount(pageCount);
		} catch (Exception e) {
			log.error(e);
			pageResponse.setCode(ResponseStatusEnum.EXCEPTION.getCode());
			pageResponse.setMsg(ResponseStatusEnum.EXCEPTION.getName());
			log.info("===========queryHtml接口返回报文："+JSONObject.toJSON(pageResponse).toString());
			return pageResponse;
		}
		pageResponse.setCode(ResponseStatusEnum.SUCCESS_CODE.getCode());
		pageResponse.setMsg(ResponseStatusEnum.SUCCESS_CODE.getName());
		log.info("===========queryHtml接口返回成功=================");
		return pageResponse;
	}
	
	
	/**
	 * 新增html列表
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "/saveHtml", method = { RequestMethod.POST })
	public DataResponse saveHtml(@RequestBody SysMaintainVUrlReq req) {
		DataResponse dataResponse = new DataResponse();
		log.info("===========saveHtml接口请求报文：" + JSONObject.toJSON(req).toString());
		if(req==null||req.getSysmaintainId()==null||req.getSysmaintainId().trim().length()<=0) {
			dataResponse.setCode(ResponseStatusEnum.PARAM_IS_NULL.getCode());
			dataResponse.setMsg(ResponseStatusEnum.PARAM_IS_NULL.getName());
			log.info("===========saveHtml接口返回报文：" + JSONObject.toJSON(dataResponse).toString());
			return dataResponse;
		}
		try {
			sysMaintainHtmlServiceImpl.saveSysHtml(req);
		} catch (Exception e) {
			log.error(e);
			dataResponse.setCode(ResponseStatusEnum.EXCEPTION.getCode());
			dataResponse.setMsg(ResponseStatusEnum.EXCEPTION.getName());
			log.info("===========saveHtml接口返回报文：" + JSONObject.toJSON(dataResponse).toString());
			return dataResponse;
		}
		dataResponse.setCode(ResponseStatusEnum.SUCCESS_CODE.getCode());
		dataResponse.setMsg(ResponseStatusEnum.SUCCESS_CODE.getName());
		log.info("===========saveHtml接口返回成功===========");
		return dataResponse;
	}
	
	
	/**
	 * 修改html列表
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "/updateHtml", method = { RequestMethod.POST })
	public DataResponse updateHtml(@RequestBody SysMaintainVUrlReq req) {
		DataResponse dataResponse = new DataResponse();
		log.info("===========updateHtml接口请求报文：" + JSONObject.toJSON(req).toString());
		if(req==null||req.getId()==null||req.getId().trim().length()<=0) {
			dataResponse.setCode(ResponseStatusEnum.PARAM_IS_NULL.getCode());
			dataResponse.setMsg(ResponseStatusEnum.PARAM_IS_NULL.getName());
			log.info("===========updateHtml接口返回报文：" + JSONObject.toJSON(dataResponse).toString());
			return dataResponse;
		}
		try {
			dataResponse = sysMaintainHtmlServiceImpl.updateSysHtml(req);
		} catch (Exception e) {
			log.error(e);
			dataResponse.setCode(ResponseStatusEnum.EXCEPTION.getCode());
			dataResponse.setMsg(ResponseStatusEnum.EXCEPTION.getName());
			log.info("===========updateHtml接口返回报文：" + JSONObject.toJSON(dataResponse).toString());
			return dataResponse;
		}
		log.info("===========updateHtml接口返回==========="+JSONObject.toJSON(dataResponse).toString());
		return dataResponse;
	}
}
